
require.config({
    baseUrl:"./modules"
})

require(["../js/ajax", "cartRender", "changeLocal"], (_, cr, cl)=>{
    let goods = localStorage.getItem("goods") ? JSON.parse(localStorage.getItem("goods")) : [];
    const tbody = document.querySelector("tbody")
    ajax({
        url: "http://localhost:3000/api",
        data:{
            type:"getGoods"
        }
    }).then(res=>{
        res = JSON.parse(res);
        // 根据指定数据，渲染到指定容器
        cr(res, goods, tbody);
    })

    // 选中单个
    tbody.addEventListener("change", function(eve){
        if(eve.target.className === "setCheck"){
            const checked = eve.target.checked;
            const id = eve.target.parentNode.parentNode.getAttribute("index")
            cl(goods, id, (i)=>{
                goods[i].flag = checked;
            })

            // 计算是否需要全选
            allCheck.checked = goods.every(val=>val.flag);
            // 计算总数量
            allNum.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num : num, 0);
            // 计算总价
            allPrice.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num*val.price : num, 0)

        }
    })
    // 修改数量
    tbody.addEventListener("input", function(eve){
        if(eve.target.className === "setNum"){
            const num = eve.target.value-0;
            const id = eve.target.parentNode.parentNode.getAttribute("index")
            cl(goods, id, (i)=>{
                goods[i].num = num;
            })

            // 计算总数量
            allNum.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num : num, 0);
            // 计算总价
            allPrice.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num*val.price : num, 0)
        }
    })
    // 删除单个
    tbody.addEventListener("click", function(eve){
        if(eve.target.className === "del"){
            eve.target.parentNode.parentNode.remove();
            const id = eve.target.parentNode.parentNode.getAttribute("index")
            cl(goods, id, (i)=>{
                goods.splice(i, 1);
            })

            // 计算是否需要全选
            allCheck.checked = goods.every(val=>val.flag);
            // 计算总数量
            allNum.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num : num, 0);
            // 计算总价
            allPrice.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num*val.price : num, 0)
        }
    })

    // 全选影响单个
    const allCheck = document.querySelector("#allCheck")
    allCheck.onclick = function(){
        // console.log(this.checked)
        const setChecks = document.querySelectorAll(".setCheck")
        setChecks.forEach(val=>{
            val.checked = this.checked;
        })
        goods.forEach(val=>val.flag=this.checked);
        localStorage.setItem("goods", JSON.stringify(goods))

        // 计算总数量
        allNum.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num : num, 0);
        // 计算总价
        allPrice.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num*val.price : num, 0)
    }
    // 单个影响全选
    // 打开页面立即判断
    allCheck.checked = goods.every(val=>val.flag);

    // 计算总数量
    const allNum = document.getElementById("allNum")
    allNum.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num : num, 0)
    
    // 计算总价
    const allPrice = document.getElementById("allPrice")
    allPrice.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num*val.price : num, 0)

    // 删除选中
    const delCheck = document.getElementById("delCheck")
    delCheck.onclick = function(){
        // 删除DOM
        const setChecks = document.querySelectorAll(".setCheck");
        setChecks.forEach(val=>{
            if(val.checked){
                val.parentNode.parentNode.remove();
            }
        })

        // 计算总数量
        allNum.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num : num, 0);
        // 计算总价
        allPrice.innerHTML = goods.reduce((num,val)=>val.flag ? num+val.num*val.price : num, 0)

        // 删除本地存储中的数据
        goods = goods.filter(val=>{
            return !val.flag;
        })
        localStorage.setItem("goods", JSON.stringify(goods));
    }
})




    

